//
//  HWGiftBaglibaoxiangqingViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/25.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWGiftBaglibaoxiangqingViewController.h"
#import "HWlibaocellTableViewCell.h"
#import "HWlibaoxiangqingjiemianModel.h"
#import "HWLoginController.h"
 
#import "HWlibaobagtankuanView.h"

@interface HWGiftBaglibaoxiangqingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UIView *huisebeijing;
@property(strong,nonatomic)UILabel *biatiuLabe;
@property(strong,nonatomic)UIView *hongsebeijingView;
@property(strong,nonatomic)UIImageView *icoIamgeView;
@property(strong,nonatomic)UILabel *mingchenglabel;
@property(strong,nonatomic) UIButton *starsBut;
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *dataMuArr;




@end

@implementation HWGiftBaglibaoxiangqingViewController



-(UITableView *)mainaTableVie {
    
    if  (_mainaTableVie == nil) {
        
        //   _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, -StatusBarHeight, kScreenW, kScreenH-TabBarHeight) style:UITableViewStylePlain];
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, StatusBarHeight+44+92, kScreenW, kScreenH- StatusBarHeight-44-92) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        
        _mainaTableVie.backgroundColor =  RGB(247, 247, 247);
        
        
        
    }
    return   _mainaTableVie ;
    
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"1111111");
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    NSLog(@"22222222");
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
    [self navigaView];
    
    
    [self shezhikongjianView];
    
    [self tianjiamnTabView];
    
    [self fasongwangluoqingqiu];
    
}



// 发送网络请求
- (void)fasongwangluoqingqiu  {
    
    
    NSDictionary *parametes = @{
        @"ct" :  @"app",
        @"ac" : @"game_lb",
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        @"gid": self.gidStr,
        @"vip": self.vip
    };
    
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        [MBProgressHUD hideHUD];
        
        
        
        
        if ([codeStr intValue] == 1) {
            //  [MBProgressHUD  showError:@"加载成功"];
            //    NSArray *arr = response[@"data"];
            self.dataMuArr = [HWlibaoxiangqingjiemianModel mj_objectArrayWithKeyValuesArray:response[@"data"][@"list"]];
            //
            //    //                  self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            //    //
            //    //                  self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            //    //
            //                      [self.maincollectionView reloadData];
            //
            NSString *iconStr = response[@"data"][@"icon"];
            
            [self.icoIamgeView sd_setImageWithURL:[NSURL URLWithString:iconStr] placeholderImage:nil];
            
            self.biatiuLabe.text = [NSString stringWithFormat:@"%@",response[@"data"][@"name"]];
            
            self.mingchenglabel.text = [NSString stringWithFormat:@"%@",response[@"data"][@"name"]];
            
            [self.starsBut setTitle:[NSString stringWithFormat:@"%@ %@",response[@"data"][@"point"],response[@"data"][@"type"]] forState:UIControlStateNormal];
            
            [self.starsBut layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
            
            [self.mainaTableVie reloadData];
            
            
            
            
            
        } else {
            
            self.dataMuArr = [[NSMutableArray alloc]init];
            
            [MBProgressHUD showError:response[@"message"]];
            
            
            
            //  [self.maincollectionView reloadData];
            
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        
        
        
    }];
    
    
    
    
}


// 添加tablview
- (void)tianjiamnTabView {
    
    [self.view addSubview:self.mainaTableVie];
    
    
}


- (void)setupUI {
    
    self.view.backgroundColor = [UIColor redColor];
    self.huisebeijing =  [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, NavBarHeight+92)];
    self.huisebeijing.backgroundColor =RGB(73, 74, 86);
    [self.view addSubview:self.huisebeijing];
    
    
}


- (void)navigaView  {
    
    //    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    button.frame = CGRectMake(10,StatusBarHeight+17, 40, 17);
    //    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    //    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    //
    //    [self.view addSubview:button];
    //
    //    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight+17, 70, 17)];
    //    textlabel.text = @"游戏详情";
    //    textlabel.textAlignment =  NSTextAlignmentCenter;
    //    textlabel.textColor = [UIColor whiteColor];
    //    textlabel.font = [UIFont systemFontOfSize:16];
    //    [self.view addSubview:textlabel];
    
    
    //    UIView *backeview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 44+StatusBarHeight+5)];
    //    backeview.backgroundColor = [UIColor whiteColor];
    //    // self.daohaView = backeview;
    //
    // //   backeview.alpha = 0.1;
    //    [self.view addSubview:backeview];
    //
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight+17, 40, 17);
    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    // self.fanhuiBut = button;
    [self.view  addSubview:button];
    
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-180)/2, StatusBarHeight+17, 180, 17)];
    textlabel.text = @"游戏详情";
    textlabel.textAlignment =  NSTextAlignmentCenter;
    textlabel.textColor = [UIColor whiteColor];
    textlabel.textColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:16];
    self.biatiuLabe  = textlabel;
    [self.view  addSubview:textlabel];
    
    
    
}


- (void)buttonTap  {
    
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

// 设置控件UIview
- (void)shezhikongjianView {
    
    
    self.hongsebeijingView =  [[UIView alloc] initWithFrame:CGRectMake(0, NavBarHeight, kScreenW, 92)];
    self.hongsebeijingView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.hongsebeijingView];
    
    
    self.icoIamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 52, 52)];
    self.icoIamgeView.layer.cornerRadius = 10;
    self.icoIamgeView.layer.masksToBounds = YES;
    self.icoIamgeView.backgroundColor = [UIColor redColor];
    [self.hongsebeijingView addSubview:self.icoIamgeView];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake( self.icoIamgeView.right+6,20, kScreenW-80, 20)];
    //     nameLabel.text = fanlilistArr[i];
    //  nameLabel.backgroundColor =  RGB(248, 119, 6);
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.textColor = RGB(255, 255, 255);
    //     nameLabel.layer.cornerRadius = 8;
    //     nameLabel.layer.masksToBounds = YES;
    nameLabel.text = @"游戏名称";
    self.mingchenglabel = nameLabel;
    [self.hongsebeijingView addSubview:nameLabel];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(kScreenW-45-20, 30, 45, 30);
    //  [button1 setBackgroundImage:[UIImage imageNamed:@"shutdownGuanbi"] forState:UIControlStateNormal];
    //  button1.backgroundColor = [UIColor redColor];
    [button1 addTarget:self action:@selector(buttonTouxiangTap) forControlEvents:UIControlEventTouchUpInside];
    //  [button1 setImage:[UIImage imageNamed:@"shutdownGuanbi"] forState:UIControlStateNormal];
    [button1  setTitle:@"详情" forState:UIControlStateNormal];
    
    [button1  setTitle:[HWjianzhiduiObject keylocalization:@"详情"] forState:UIControlStateNormal];
    
    
    [button1   setTitleColor:[UIColor  whiteColor] forState:UIControlStateNormal];
    button1.backgroundColor = RGB(246, 106, 5);
    button1.layer.cornerRadius = 15;
    button1.layer.masksToBounds = YES;
    button1.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.hongsebeijingView addSubview:button1];
    
    
    self.starsBut = [UIButton  createNormalButtonWithFrame:CGRectMake(self.icoIamgeView.right+6,nameLabel.bottom+3, 15+8+3+40, 14) Image:@"stars_ico" Title:@"8.0" font:10 imageSize:CGSizeMake(8, 8)];
    [self.starsBut layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
    [self.starsBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.hongsebeijingView addSubview: self.starsBut];
    
    
    
    
    
}




- (void)buttonTouxiangTap  {
    
    
    NSLog(@"1234554");
    
    HWGameDetailController *gameDetail = [[HWGameDetailController alloc]init];
    gameDetail.gameId = self.gidStr;
    [self.navigationController pushViewController:gameDetail animated:YES];
    
    
    
    
    
}








- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return   self.dataMuArr.count;
    
    
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"YouXiID";
    HWlibaocellTableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWlibaocellTableViewCell" owner:nil options:nil].firstObject;
        
    }
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
    HWlibaoxiangqingjiemianModel *mdoel = self.dataMuArr[indexPath.row];
    
    [cell.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:mdoel.bgimg] placeholderImage:nil];
    cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",mdoel.cardname];
    cell.jieshaoLabel.text = [NSString stringWithFormat:@"%@",mdoel.content];
    
    //    CGFloat  bfb = [mdoel.bfb floatValue]/100;
    //    cell.jingdutiaoProgreaView.progress = bfb;
    //    cell.shengyuLabel.text = [NSString stringWithFormat:@"剩余 %d%%",100 - [mdoel.bfb intValue]];
    //
    
    CGFloat  bfb = [mdoel.bfb floatValue]/100;
    cell.jingdutiaoProgreaView.progress = bfb;
    cell.shengyuLabel.text = [NSString stringWithFormat:@"剩余 %d%%",[mdoel.bfb intValue]];
    
    
    [cell.beijingyanseBut addTarget:self action:@selector(beijingyanseButTap:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    return  cell;
    
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 92+5;
    
    
    
    
    
}




- (void)beijingyanseButTap:(UIButton *)sender {
    
    NSLog(@"%@",[[[sender superview] superview]superview]);
    
    HWlibaocellTableViewCell *cell = (HWlibaocellTableViewCell *)[[[sender superview] superview]superview];
    
    
    NSIndexPath *index =  [self.mainaTableVie indexPathForCell:cell];
    
    
    HWlibaoxiangqingjiemianModel *mdoel =   self.dataMuArr[index.row ];
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        
        
        HWLoginController  *login = [[HWLoginController alloc]init];
        [self.navigationController pushViewController:login animated:YES];
        return;
        
        
        
        
    }
    
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    
    
    
    
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    
    NSDictionary *parametes = @{
        @"ct" :  @"app",
        @"ac" : @"get_lb",
        @"uid":userID,
        @"t":tStr,
        @"sign":signMD5,
        @"id": mdoel.id,
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        
        
    };
    
    
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        [MBProgressHUD hideHUD];
        
        
        
        
        if ([codeStr intValue] == 1) {
            
            
            //   [MBProgressHUD showError:response[@"message"]];
            
            
            HWlibaobagtankuanView  *biaoqian = [[HWlibaobagtankuanView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
            
            biaoqian.zifuchuanStr  = [NSString stringWithFormat:@"%@",response[@"data"]];
            
            [kKeyWindow addSubview:biaoqian];
            
            
            
        } else {
            
            
            
            [MBProgressHUD showError:response[@"message"]];
            
            
            
            //  [self.maincollectionView reloadData];
            
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        
        
        
    }];
    
    
    
}






@end
